package org.nobody.mallb2c.mallproduct.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.nobody.mallb2c.mallproduct.entity.BrandEntity;
import org.nobody.mallb2c.mallproduct.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.nobody.mallb2c.mallproduct.entity.CategoryBrandRelationEntity;
import org.nobody.mallb2c.mallproduct.service.CategoryBrandRelationService;
import org.nobody.common.utils.PageUtils;
import org.nobody.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author fancier
 * @email fancier2093@gmail.com
 * @date 2020-07-30 16:25:26
 */
@RestController
@RequestMapping("mallproduct/categorybrandrelation")
public class CategoryBrandRelationController {
  @Autowired
  private CategoryBrandRelationService categoryBrandRelationService;

  /**
   * 获取当前品牌关联的所有分类列表
   */
  @GetMapping(value = "/catelog/list")
  public R cateloglist(@RequestParam("brandId") Long brandId) {
    List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
      new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId)
    );

    return R.ok().put("data", data);
  }

  /**
   * 根据catId获取商品品牌信息
   * @param catId
   * @return
   */
  @GetMapping("/brands/list")
  public R relationBrandsList(@RequestParam(value = "catId",required = true) Long catId){
    List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId);
    List<BrandVo> collect = vos.stream().map(item -> {
      BrandVo brandVo = new BrandVo();
      brandVo.setBrandId(item.getBrandId());
      brandVo.setBrandName(item.getName());
      return brandVo;
    }).collect(Collectors.toList());
    return R.ok().put("data", collect);
  }


  /**
   * 列表
   */
  @RequestMapping("/list")
  //@RequiresPermissions("mallproduct:categorybrandrelation:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = categoryBrandRelationService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{id}")
  //@RequiresPermissions("mallproduct:categorybrandrelation:info")
  public R info(@PathVariable("id") Long id) {
    CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

    return R.ok().put("categoryBrandRelation", categoryBrandRelation);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("mallproduct:categorybrandrelation:save")
  public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
    categoryBrandRelationService.saveDetail(categoryBrandRelation);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("mallproduct:categorybrandrelation:update")
  public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
    categoryBrandRelationService.updateById(categoryBrandRelation);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("mallproduct:categorybrandrelation:delete")
  public R delete(@RequestBody Long[] ids) {
    categoryBrandRelationService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

}
